﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework.Controls;

namespace VideoPlayer
{
    public partial class BottomPlay : MetroUserControl
    {
        public BottomPlay()
        {
            InitializeComponent();
            //this.StartOrStopAction = StartOrStopAction;
            //this.SkipTimeAction = SkipTimeAction;
        }

        /// <summary>
        /// 是否暂停
        /// </summary>
        public bool IsStop { get; private set; } = true;

        /// <summary>
        /// 当前播放时间
        /// </summary>
        public int IndexTime { get; set; } = 0;

        /// <summary>
        /// 快进 快退一次时间 单位 s
        /// </summary>
        public int SkipTime { get; set; } = 5;


        /// <summary>
        /// 默认加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BottomPlay_Load(object sender, EventArgs e)
        {
            SetBtnStyle(btnStart, btnRetreat, btnForward);
        }
        #region 暴露的方法

        /// <summary>
        /// 设置最大时间 单位 秒
        /// </summary>
        /// <param name="time"></param>
        public void SetMaxTime(int time)
        {
            time = time == 0 ? int.MaxValue: time;
            trackTime.Maximum = time;
            allTime.Text = TimeConvert(time);
        }

        /// <summary>
        /// 设置当前播放事件 单位 秒
        /// </summary>
        /// <param name="time"></param>
        public void SetIndexTime(int time)
        {
            trackTime.Value = time;
            IndexTime = time;
            playTime.Text = TimeConvert(time);
        }

        /// <summary>
        /// 开始
        /// </summary>
        public void Start()
        {
            IsStop = false;
            if (IsStop)
                btnStart.BackgroundImage = Properties.Resources.Start;
            else
                btnStart.BackgroundImage = Properties.Resources.Stop;
        }

        /// <summary>
        /// 暂停
        /// </summary>
        public void Stop()
        {
            IsStop = true;
            if (IsStop)
                btnStart.BackgroundImage = Properties.Resources.Start;
            else
                btnStart.BackgroundImage = Properties.Resources.Stop;
        }

        #endregion

        #region 委托方法

        /// <summary>
        /// 暂停 或 开始  true 为暂停
        /// </summary>
        public Action<bool> StartOrStopAction;

        /// <summary>
        /// 跳转时间
        /// </summary>
        public Action<int> SkipTimeAction;

        #endregion

        #region 修改样式
        /// <summary>  
        /// 设置透明按钮样式  
        /// </summary>  
        private void SetBtnStyle(params Button[] btns)
        {
            foreach (Button btn in btns)
            {
                //btn.FlatStyle = FlatStyle.Flat;//样式  
                //btn.ForeColor = Color.Transparent;//前景  
                //btn.BackColor = Color.Transparent;//去背景  
                //btn.FlatAppearance.BorderSize = 0;//去边线  
                //btn.ForeColor = Color.Black;
                btn.FlatAppearance.MouseOverBackColor = Color.Transparent;//鼠标经过  
                btn.FlatAppearance.MouseDownBackColor = Color.Transparent;//鼠标按下 


            }
        }
        #endregion

        #region 按钮移入移出事件

        /// <summary>
        /// 快进鼠标移入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnForward_MouseEnter(object sender, EventArgs e)
        {
            Button b = sender as Button;
            b.BackgroundImage = Properties.Resources.Forward_1;
        }

        /// <summary>
        /// 快进鼠标移出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnForward_MouseLeave(object sender, EventArgs e)
        {
            Button b = sender as Button;
            b.BackgroundImage = Properties.Resources.Forward;
        }


        /// <summary>
        /// 开始/暂停 鼠标移入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnStart_MouseEnter(object sender, EventArgs e)
        {
            Button b = sender as Button;
            if (IsStop)
                b.BackgroundImage = Properties.Resources.Start_1;
            else
                b.BackgroundImage = Properties.Resources.Stop_1;
        }

        /// <summary>
        /// 开始/暂停 鼠标移出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnStart_MouseLeave(object sender, EventArgs e)
        {
            Button b = sender as Button;
            if (IsStop)
                b.BackgroundImage = Properties.Resources.Start;
            else
                b.BackgroundImage = Properties.Resources.Stop;
        }

        /// <summary>
        /// 快退鼠标移入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnRetreat_MouseEnter(object sender, EventArgs e)
        {
            Button b = sender as Button;
            b.BackgroundImage = Properties.Resources.Retreat_1;
        }

        /// <summary>
        /// 快退鼠标移出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnRetreat_MouseLeave(object sender, EventArgs e)
        {
            Button b = sender as Button;
            b.BackgroundImage = Properties.Resources.Retreat;
        }

        #endregion

        #region 点击事件

        /// <summary>
        /// 开始/暂停
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnStart_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            IsStop = !IsStop;
            if (IsStop)
                b.BackgroundImage = Properties.Resources.Start_1;
            else
                b.BackgroundImage = Properties.Resources.Stop_1;
            StartOrStopAction(IsStop);
        }

        /// <summary>
        /// 快进
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnForward_Click(object sender, EventArgs e)
        {
            if ((IndexTime + SkipTime) >= trackTime.Maximum)
                IndexTime = trackTime.Value - 1;
            else
                IndexTime += SkipTime;
            SkipTimeAction(IndexTime);
        }

        /// <summary>
        /// 快退
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnRetreat_Click(object sender, EventArgs e)
        {
            if ((IndexTime - SkipTime) <= 0)
                IndexTime = 0;
            else
                IndexTime -= SkipTime;
            SkipTimeAction(IndexTime);
        }


        /// <summary>
        /// 鼠标点击释放 改变进度条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TrackTime_MouseUp(object sender, MouseEventArgs e)
        {
            IndexTime = trackTime.Value;
            SkipTimeAction(IndexTime);
        }

        #endregion

        #region 其他方法

        /// <summary>
        /// 扩展方法 把int转换成00:00:00时间格式
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        private string TimeConvert(int i)
        {
            string result = "";
            if (i % 3600 != 0)
            {
                result = (i / 3600).ToString("00") + ":";
                i = i % 3600;
                if (i % 60 != 0)
                {
                    result += ((i / 60).ToString("00") + ":");
                    result += (i % 60).ToString("00");
                }
                else
                    result += ((i / 60).ToString("00") + ":00");
            }
            else
                result = (i / 3600).ToString("00") + ":00:00";

            return result;
        }

        #endregion


    }
}
